<?php
/***********************************************************************
 * @package     IvanEngine
 * @subpackage  Core
 * @author      Ivan <ivanzx@msn.com>
 * @created     2010-06-06
 ***********************************************************************/
if(defined("IVANENGINE_CORE_JSHELPER"))
    die("Redefinition macro in :".__FILE__."(".__LINE__.")");
define("IVANENGINE_CORE_JSHELPER", true);

require_once (dirname(__FILE__).'/../Common.php');
require_once (dirname(__FILE__).'/FileHelper.php');

class CIncludeHelper extends CSingleton
{
    static protected $m_this= null;
    protected   
        $m_jsFiles        = null,
        $m_cssFiles       = null;
    public function __construct()
    {
        parent::__construct();
        $this->m_jsFiles      = new CVector();
        $this->m_cssFiles     = new CVector();
        $this->JS(M_CORE_PATH.'JS'.M_DS.'jquery.js');
        $this->JS(M_CORE_PATH.'JS'.M_DS.'jquery-ui.js');
    }
    
    public function __destruct()
    {
        unset($this->m_jsFiles);
        unset($this->m_cssFiles);
        parent::__destruct();
    }
    
    static public function Get()
    {
        if( self::$m_this == null )
        {
            parent::StartSingleton();
            self::$m_this = new CIncludeHelper();
            parent::EndSingleton();
        }
        return self::$m_this;
    }

    public function JS($fileName)
    {
        $relFileName = CFileHelper::AToR($fileName);
        $relFileName = strtolower($relFileName);
        if( $this->m_jsFiles->Has($relFileName) ){
            return true;
        }
        if( CFileHelper::IsExsit($fileName) ){
            $this->m_jsFiles->Add($relFileName);
            return true;
        } 
        
        return false;
    }
    
    public function CSS($fileName)
    {
        $relFileName = CFileHelper::AToR($fileName);
        $relFileName = strtolower($relFileName);
        if( $this->m_cssFiles->Has($relFileName) ){
            return true;
        }
        if( CFileHelper::IsExsit($fileName) ){
            $this->m_cssFiles->Add($relFileName);
            return true;
        } 
        
        return false;
    }
    
    public function & GetJS()
    {
        return $this->m_jsFiles;
    }
    
    public function & GetCSS()
    {
        return $this->m_cssFiles;
    }
    
    public function GetJSText()
    {
        $result = '';
        $it = $this->m_jsFiles->Begin();
        for( ; !$it->IsEnd(); $it->Next() )
        {
            $result .= '<script type="text/javascript" src="'.$it->Value().'"></script>'.M_WARP;
        } 
        return $result;
    }
    
    public function GetCSSText()
    {
        $result = '';
        $it = $this->m_cssFiles->Begin();
        for( ; !$it->IsEnd(); $it->Next() )
        {
            $result .= '<link type="text/css" rel="stylesheet" href="'.$it->Value().'"/>'.M_WARP;
        }
        return $result;
    }
}

class HInclude extends CIncludeHelper{}
?>
